﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ColorRichTextBox.ColorRichTextBox;
using Ionic.Zip;
using System.IO;
using GAM_Web_Designer_Pro.Properties;
using System.Runtime.Serialization.Formatters.Binary;

namespace GAM_Web_Designer_Pro.Forms {
    public partial class NewProjectForm : Form {

        private List<ProjectTemplate> _templates;
        private Project _project;

        public Project CurrentProject {
            get {
                return _project;
            }
            set {
                _project = value;
            }
        }

        public NewProjectForm() {
            InitializeComponent();
            _templates = ProjectTemplateAccess.LoadAllProjectTemplates();
        }

        private void browseButton_Click( object sender, EventArgs e ) {
            if ( folderBrowserDialog1.ShowDialog( this ) == DialogResult.OK )
                _dirTB.Text = folderBrowserDialog1.SelectedPath;
        }

        private void createButton_Click( object sender, EventArgs e ) {
            if ( VerifyInfo() ) {
                try {

                    string dataDir = _dirTB.Text.Trim() + @"\" + _nameTB.Text.Trim();

                    if ( !Directory.Exists( dataDir ) )
                        Directory.CreateDirectory( dataDir );

                    Project proj = new Project();
                    ProjectTemplate temp = _templates[ _tempsLB.SelectedIndex ];
                    ZipFile zip = new ZipFile( temp.ZipFile );
                    zip.Password = Settings.Default.ProjectZipPass;
                    zip.Encryption = EncryptionAlgorithm.WinZipAes128;
                    zip.ExtractAll( dataDir, ExtractExistingFileAction.OverwriteSilently );

                    proj.Directory = dataDir;
                    proj.Language = temp.Language;
                    proj.Name = _nameTB.Text.Trim();

                    FileStream stream = File.Create( proj.Directory + "\\" + proj.Name + Settings.Default.ProjectExt );
                    BinaryFormatter formatter = new BinaryFormatter();

                    formatter.Serialize( stream, proj );
                    stream.Close();

                    MessageBox.Show( this, "Project Created!", "", MessageBoxButtons.OK, MessageBoxIcon.Information );
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch ( Exception ex ) {
                    new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
                }
            }
        }

        private void cancelbutton_Click( object sender, EventArgs e ) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void NewProjectForm_Load( object sender, EventArgs e ) {
            foreach ( ProjectTemplate item in _templates ) {
                _tempsLB.Items.Add( item.Name );
            }
            _dirTB.Text = Environment.GetEnvironmentVariable( "USERPROFILE" ) + "\\Documents\\GAM WD Projects";
        }

        private bool VerifyInfo() {
            if ( _nameTB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "You must enter a Name for your Project", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else if ( _dirTB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "You must select a directory to store the project files.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else if ( _tempsLB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "You must select a template for your Project", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else
                return true;
        }
    }
}
